<?php

namespace app\admin\controller;
use think\Db;

class Community extends Common{


	// public function news(){
	// 	return $this->fetch();
	// }
	// /*
	// 数据接口
	// */
	// public function newsList(){
	// 	$limit = request()->param('limit')?:10;
	// 	$data  = Db::table('news')
	// 			->order('time desc')
	// 			->paginate($limit);
	// 	$data = $data->toArray();
	// 	return $this->tableData($data);
	// }
	// 	/*
	// 编辑新闻
	//  */
	// public function editNews(){
	// 	if (request()->method() == 'POST') {
	// 		$data = [];
	// 		$data['id'] = request()->param('id');
	// 		$data['title'] = request()->param('title');
	// 		$data['time'] = request()->param('time');
	// 		$data['url'] = request()->param('url');
	// 		$res  = Db::table('news')->where('id',$data['id'])->update($data);
	// 		if ($res) {
	// 			return ['code'=>1,'msg'=>'success'];
	// 		} else {
	// 			return ['code'=>0,'msg'=>'更新失败'];
	// 		}			
	// 	} else {
	// 		$id = request()->param('id');
	// 		$data = Db::table('news')
	// 					->where('id',$id)
	// 					->find();
	// 		$this->assign('data',$data);
	// 		return $this->fetch();
	// 	}
	// }
	///*
	// 新增新闻
	//  */
	// public function addNews(){
	// 	if (request()->method() == 'POST') {
	// 		$data = [];
	// 		$data['title'] = request()->param('title');
	// 		$data['time'] = request()->param('time');
	// 		$data['url'] = request()->param('url');
	// 		$res  = Db::table('news')->insert($data);
	// 		if ($res) {
	// 			return ['code'=>1,'msg'=>'success'];
	// 		} else {
	// 			return ['code'=>0,'msg'=>'更新失败'];
	// 		}			
	// 	} else {
	// 		return $this->fetch();
	// 	}
	// }
	// 	/*
	// 删除新闻
	//  */
	// public function delNews(){
	// 	$id = request()->param('id');
	// 	$res = Db::table('news')->where('id',$id)->delete();
	// 	if ($res) {
	// 		return ['code'=>1,'msg'=>'successs'];
	// 	} else {
	// 		return ['code'=>0,'msg'=>'删除失败'];
	// 	}
	// }
	// 
	// 
/////////////////////////////////////////////////


	//应用下载 
	public function app(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function appList(){
		$limit = request()->param('limit')?:10;
		$type     = request()->param('type');
		$where      = '1=1';
        if($type != '') {
            $where .= ' and type='.$type;
        }
		$data  = Db::table('tp_community_application')
				->where($where)
				->paginate($limit);
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['pic'] = $this->imageUrl($val['pic']);
		}
		return $this->tableData($data);
	}
	
	/*
	编辑应用
	 */
	public function editApp(){
		if (request()->method() == 'POST') {
			$data = [];
			$id = request()->param('id');
			$data['name'] = request()->param('name');
			$data['pic']  = request()->param('pic');
			$data['url'] = request()->param('url');
			$data['ms'] = request()->param('ms');
			$data['type'] = request()->param('type')?:1;
			$data['status'] = request()->param('status');
			if ($id) {
				$res  = Db::table('tp_community_application')->where('id',$id)->update($data);
			} else {
				$res  = Db::table('tp_community_application')->insert($data);
			}
			
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id = request()->param('id');
			$data = [];
			$data['status'] = 1;
			$data['type'] = 1;
			if ($id) {
				$data = Db::table('tp_community_application')
						->where('id',$id)
						->find();
			}
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	/*
	删除应用程序
	 */
	public function delApp(){
		$id = request()->param('id');
		$res = Db::table('tp_community_application')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'删除失败'];
		}
	}

/////////////////////////////////////////////////////////////

	/*
	平台咨询
	 */
	public function announce(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function announceList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_community_announce')
				->paginate($limit);
		$data = $data->toArray();
		// foreach ($data['data'] as $key => $val) {
		// 	$data['data'][$key]['pic'] = $this->imageUrl($val['pic']);
		// }
		return $this->tableData($data);
	}
	/*
	删除平台咨询
	 */
	public function delAnnounce(){
		$id = request()->param('id');
		$res = Db::table('tp_community_announce')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>1,'msg'=>'successs'];
		} else {
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	/*
	编辑平台咨询
	 */
	public function editAnnounce(){
		if (request()->method() == 'POST') {
			$data = [];
			$id = request()->param('id');
			$data['title'] = request()->param('title');
			$data['ms'] = request()->param('ms');
			$data['pic'] = request()->param('pic');
			$data['content'] = input('post.content','',null);
			//return $data['content'];
			$data['status'] = 1;
			$data['time'] = date('Y-m-d H:i:s',time());
			if ($id) {
				$res  =Db::table('tp_community_announce')->where('id',$id)->update($data);
			} else {
				$res  =Db::table('tp_community_announce')->insert($data);
			}
			
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id = request()->param('id');
			$data = [];
			if ($id) {
				$data  = Db::table('tp_community_announce')->where('id',$id)->find();
			}
			 
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

//////////////////////////////////////////////////////////////

	//社区轮播图
	public function slideshow(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function slideshowList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_community_slideshow')
				->paginate($limit);
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['pic'] = $this->imageUrl($val['pic']);
		}
		return $this->tableData($data);
	}
	/*
	删除社区轮播图
	 */
	public function delSlideshow(){
		$id = request()->param('id');
		$res = Db::table('tp_community_slideshow')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'删除失败'];
		}
	}
	/*
	新增社区轮播图
	 */
	public function addSlideshow(){
		if (request()->method() == 'POST') {
			$data = [];
			$data['pic']  = request()->param('pic');

			$data['status'] = 1;
			$res  = Db::table('tp_community_slideshow')->insert($data);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			return $this->fetch();
		}
	}
//////////////////////////////////////////////

	public function upload_img() {
		$type       = P('type')?:1;
		// $image      = $_FILES["file"];
		$image      =request()->file('file');
		$uid = $this->uid;
		if (empty($uid)) {
        	return ['code'=>1,'msg'=>'缺少参数'];

			// $this->retData(1, '缺少参数');
		}
		switch ($type) {
			case 1:
				$path = '/upload/';
				break;
			
			default:
				# code...
				break;
		}
		// var_dump($image);die();
		$validate = ['size'=>20*1024*1024,'ext'=>'jpg,png,gif,jpeg,bmp,png'];
		$res = $image->validate($validate)->move('../public'.$path);
		if ($res) {
			// var_dump($res);die();
            $name = $res->getSaveName();
			// $name = $res->filename();
			$save_name = '../public'.$path.$name;
            $name = str_replace('\\', '/', $name);
            //图片压缩
            // $compress = new \Picture\Compress($save_name,0.5);
            // $img_name = explode('.', $name);
            // $new_name = $img_name[0].'2.'.$img_name[1];
            // // $compress->compressImg('../public'.$path.$new_name);
            // $name = $path.$new_name;
            return ['code'=>0,'msg'=>'successs','data'=>'/upload/'.$name];
            // $this->retData(0,'success',$name);
        } else {
        	return ['code'=>1,'msg'=>'图片上传失败'];
            // $this->retData(1,'图片上传失败');
        }
	}
}